#include <iostream>
#include <vector>

struct Node
{
    std::string addr;
    int data;
    std::string next_addr;
};

main()
{
    std::string head_node;
    int node_num;
    std::cin >> head_node >> node_num;

    std::vector<Node> node_vec(node_num);
    for (int i = 0; i < node_num; i++)
    {
        std::cin >> node_vec[i].addr >> node_vec[i].data >> node_vec[i].next_addr;
    }

    int cnt = node_num / 2;

    int data;
    while (cnt >= 0)
    {
        for (int i = 0; i < node_num; i++)
        {
            if (node_vec[i].addr == head_node)
            {
                cnt--;
                head_node = node_vec[i].next_addr;
                data = node_vec[i].data;
                break;
            }
        }
    }
    std::cout << data << std::endl;
    return 0;
}